package admin

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"fmt"
	"github.com/astaxie/beego"
	"html/template"
	"strconv"
)

type FinanceController struct {
	BaseController
}

func (c *FinanceController) ShowList() {
	curPage, _ := strconv.Atoi(c.GetString("p"))
	if curPage <= 0 {
		curPage = 1
	}
	perCount, _ := beego.AppConfig.Int("percount")
	symPageCount, _ := beego.AppConfig.Int("symmetricpagecount")
	p := map[string]interface{}{}
	mType := c.GetString("type")
	if len(mType) > 0 {
		p["type"] = mType
		c.Data["type"], _ = strconv.Atoi(mType)
	} else {
		c.Data["type"] = -1
	}
	realName := c.GetString("real_name")
	c.Data["realName"] = realName
	if len(realName) > 0 {
		p["realName"] = realName
	}
	phone := c.GetString("phone")
	c.Data["phone"] = phone
	if len(phone) > 0 {
		p["phone"] = phone
	}
	startTime := c.GetString("start_time")
	c.Data["startTime"] = startTime
	if len(startTime) > 0 {
		p["startTime"] = fmt.Sprintf("%s 00:00:00", startTime)
	}
	endTime := c.GetString("end_time")
	c.Data["endTime"] = endTime
	if len(endTime) > 0 {
		p["endTime"] = fmt.Sprintf("%s 23:59:59", endTime)
	}
	p["relation"] = true
	p["convert"] = true
	p["curPage"] = curPage
	p["perCount"] = perCount
	c.Data["totalAmount"] = service.GetFinanceTotalAcmount(p)
	totalCount, recordList := service.GetFinanceList(p)
	paginator := utils.NewPaginator(int(totalCount), perCount, symPageCount, curPage)
	c.Data["paginator"] = paginator.GetPageHtml()
	c.Data["recordList"] = recordList
	c.Data["xsrfdata"] = template.HTML(c.XSRFFormHTML())
	c.Data["typeList"] = models.FinanceType
	c.TplName = "admin/finance/list.html"
}
